<template>
  <div class="wrap">
    <div class="title">
      <em class="look" :class="{ activTitle: whichTitle == 0 }" @click="getUserFollowList(0)">关注</em>
      <em class="bestNew" :class="{ activTitle: whichTitle == 1 }" @click="getNewestList(1)">最新</em>
    </div>
    <div class="container">

      <div  v-for="(item, index) in resForumList" :key="index">
        <!-- .center 取消置顶 -->
        <div class="center" v-if="index==0">
          <div class="items" @click="jumpForumInfo(item.id)">
            <div class="top">置顶</div>

            <van-image radius="10" width="100%"    fit="cover" :src="$imgUrl + item.imageUrl[0]" v-if="item.imageUrl[0]" />
            <div class="forumUserList">
              <div class="forumTitle">
                {{ item.content }}
              </div>
              <div class="forumBottom">
                <div class="forumLeft">
                  <img :src="$imgUrl + item.headImage" alt="" class="userHead" />
                  <em class="userName">{{ item.nickName }}</em>
                </div>
                <div class="forumRights">
                  <img src="@/assets/greyHeart.png" alt="" class="redHeart" v-if="item.likeNum == 0" />
                  <img src="@/assets/redHeart.png" alt="" class="redHeart" v-else />
                  <em class="loveNumber">{{ item.likeNum }}</em>
                </div>
              </div>
            </div>
          </div>
        </div>
            <!--取消置顶  -->
          <!-- <div class="left" v-if="index%2!=0"> -->
        <div class="left" v-if="index!=0&index%2!=0">
          <div class="item" @click="jumpForumInfo(item.id)">
            <van-image radius="10" width="90%"  height="140"  fit="cover" :src="$imgUrl + item.imageUrl[0]" v-if="item.imageUrl[0]" />
            <!-- <img :src="$imgUrl+item.imageUrl[0]" alt="" class="forunmImg" v-if="item.imageUrl[0]" /> -->
            <div class="forumUserList">
              <div class="forumTitle">
                {{ item.content }}
              </div>
              <div class="forumBottom">
                <div class="forumLeft">
                  <img :src="$imgUrl + item.headImage" alt="" class="userHead" />
                  <em class="userName">{{ item.nickName }}</em>
                </div>
                <div class="forumRight">
                  <img src="@/assets/greyHeart.png" alt="" class="redHeart" v-if="item.likeNum == 0" />
                  <img src="@/assets/redHeart.png" alt="" class="redHeart" v-else />
                  <em class="loveNumber">{{ item.likeNum }}</em>
                </div>
              </div>
            </div>
          </div>
        </div>
        <!-- 取消置顶 -->
        <!-- <div class="right"  v-if="index%2==0"> -->
          <div class="right" v-if="index!=0&index%2==0">
          <div class="item" @click="jumpForumInfo(item.id)">
             <van-image  radius="10" width="90%"   height="140" fit="cover" :src="$imgUrl + item.imageUrl[0]" v-if="item.imageUrl[0]" />
            <!-- <img :src="$imgUrl + item.imageUrl[0]" alt="" class="forunmImg" v-if="item.imageUrl[0]" /> -->
            <div class="forumUserList">
              <div class="forumTitle">
                {{ item.content }}
              </div>
              <div class="forumBottom">
                <div class="forumLeft">
                  <img :src="$imgUrl + item.headImage" alt="" class="userHead" />
                  <em class="userName">{{ item.nickName }}</em>
                </div>
                <div class="forumRight">
                  <img src="@/assets/greyHeart.png" alt="" class="redHeart" v-if="item.likeNum == 0" />
                  <img src="@/assets/redHeart.png" alt="" class="redHeart" v-else />
                  <em class="loveNumber">{{ item.likeNum }}</em>
                </div>
              </div>
            </div>
          </div>
        </div> 
      </div>
    </div>
    <div class="sendInvitationWrap">
      <div class="sendItem" @click="jumpPostForum">发帖</div>
      <div class="msgItem" @click="jumpMsgInfo">
        <em class="msgText">消息</em>
        <em class="redPoint" v-if="!msgStatus"></em>
      </div>
    </div>
    <!-- <tabbar></tabbar> -->
    <div style="height: 100px"></div>
    <div class="tabbarWrap">
      <router-link :to="{ name: 'index' }">
        <div class="tabbarItem">
          <img src="@/assets/tabbar1.1.png" alt="" class="tabbarImg" />
          <p class="tabbarText">首页</p>
        </div>
      </router-link>
      <router-link :to="{ name: 'forumIndex' }">
        <div class="tabbarItem">
          <img src="@/assets/tabbar2.2.png" alt="" class="tabbarImg" />
          <p class="tabbarText">论坛</p>
        </div>
      </router-link>
      <router-link :to="{ name: 'personalCenter' }">
        <div class="tabbarItem">
          <img src="@/assets/tabbar3.1.png" alt="" class="tabbarImg" />
          <p class="tabbarText">我的</p>
        </div>
      </router-link>
    </div>

    <div v-if="pulldown" @click="top" class="pulldown"><img class="img_back" src="@/assets/backTop.png" alt=""></div>

  </div>
</template>

<script>
import { Switch, Image as VanImage } from 'vant';
import tabbar from '../components/tabbar';
import { apiUserFollowList, apiNewestList, apiGetMessageStatus } from '@/request/api'; // api接口
export default {
  name: 'App',
  data() {
    return {
      resForumList: [], //帖子列表
      whichTitle: 0,
      reqpageSize: 5, //分页条数
      reqPageNum: 0, //当前第几页
      noMore: false,
      msgStatus: '', //是否有未读消息，true没有,false有
      Top:"",
      WinH:'',
      DocH:"",


      pulldown:false


    };
  },
  created() {},
  activated(){
    console.log($(window).scrollTop());
    this.Top = 0
      this.WinH =0
      this.DocH =0

   window.addEventListener('scroll', this.handleScroll, true);
    // this.getUserFollowList(0);
    this.getNewestList(1);
    this.getMessageStatus();
  },
    // 跳转到指定界面
      beforeRouteEnter(to, from, next) {
      console.log(to);
        next(vm => {
      // 回到原来的位置
      const position = JSON.parse(window.sessionStorage.getItem('position2'))
      console.log(position);
      window.scrollTo(0,position)
    })
      

  },
  beforeRouteLeave(to, from, next) {
    console.log(to);
    // 保存离开页面时的位置
    const position = window.scrollY
    window.sessionStorage.setItem('position2', JSON.stringify(position))
    next()
  },
  mounted() {
    this.Top = 0
      this.WinH =0
      this.DocH =0

    this.resForumList = [];
    document.querySelector('body').setAttribute('style', 'background-color:#F2F4FA');
    window.addEventListener('scroll', this.handleScroll, true);
    // this.getUserFollowList(0);
    this.getNewestList(1);
    this.getMessageStatus();
  },
  methods: {
     top(){
   window.scrollTo(0,0)
     },
    //分页 监听滑动事件
    handleScroll() {

      this.Top = $(window).scrollTop();
      this.WinH = $(window).height();
      this.DocH = $(document).height();

      if(this.Top>1000){
        this.pulldown=true
      }else{
        this.pulldown=false
      }

      // console.log(this.Top,this.WinH,this.DocH);

      if (this.DocH - (this.Top + this.WinH) <= 0) {
        if (this.noMore) {
          return;
        }
        if (this.whichTitle == 0) {
          this.noMore = false;
          this.getUserFollowList(0);
        } else {
          this.noMore = false;
          this.getNewestList(1);
        }
      }
    },
    //获取关注用户的帖子列表：
    getUserFollowList(i) {
      if (this.whichTitle !== i) {
        this.resForumList = [];
        this.whichTitle = i;
        this.reqPageNum = 0;
        this.noMore = false;
      }
      var page = (this.reqPageNum += 1);
      apiUserFollowList({
        pageNum: page,
        pageSize: this.reqpageSize,
      }).then((res) => {
        if (res.data.rows.length === 0) {
          this.noMore = true;
          return;
        }
        console.log(res);
        var dataRes = res.data.rows;
        dataRes.map((item) => {
          item.imageUrl = item.imageUrl.split(',');
        });
        this.resForumList = [...this.resForumList, ...dataRes];
      });
    },
    //获取最新的帖子列表：
    getNewestList(i) {
      if (this.whichTitle !== i) {
        this.resForumList = [];
        this.whichTitle = i;
        this.reqPageNum = 0;
        this.noMore = false;
      }
      var page = (this.reqPageNum += 1);
      apiNewestList({
        pageNum: page,
        pageSize: this.reqpageSize,
      }).then((res) => {
        if (res.data.rows.length === 0) {
          this.noMore = true;
          return;
        }
        var dataRes = res.data.rows;
        dataRes.map((item) => {
          item.imageUrl = item.imageUrl.split(',');
        });
        this.resForumList = [...this.resForumList, ...dataRes];
      });
      console.log('resForumList最新帖子', this.resForumList);
    },

    //获取最新的帖子列表：
    getMessageStatus() {
      apiGetMessageStatus({}).then((res) => {
        this.msgStatus = res.data.msgStatus;
      });
    },
    jumpForumInfo(i) {
      this.$router.push({
        path: '/pages/forumInfo',
        query: {
          //传递参数
          forumId: i,
        },
      });
    },
    jumpMsgInfo() {
      this.$router.push({
        path: '/pages/msgIndex',
        query: {
          //传递参数
        },
      });
    },
    jumpPostForum() {
      this.$router.push({
        path: '/pages/postForum',
        query: {
          //传递参数
        },
      });
    },
  },
  deactivated() {
    window.removeEventListener('scroll', this.handleScroll, true);
  },
  components: {
    tabbar,
    [Switch.name]: Switch,
    VanImage,
  },
};
</script>

<style scoped>
.wrap {
  width: 100%;
  margin-top: 10px;
  background: #ffffff;
  min-height: 100vh;
  border-radius: 20px 20px 0px 0px;
}
.title {
  font-size: 14px;
  font-weight: 500;
  color: #333333;
  width: 93%;
  margin: 0px auto;
  padding: 15px 0px;
}
.look {
}

.bestNew {
  padding-left: 25;
}
.activTitle {
  color: #181818;
  font-size: 16px;
  font-weight: 600;
}
.container {
  width: 93%;
  margin: 0px auto;
  overflow: hidden;
}
.left {
  float: left;
  width: 48.5%;
}
.item {
  margin-bottom: 20px;
    text-align: center!important;
}
.forunmImg {
  width: 167px;
  height: auto;
  border-radius: 20px;
}
.forumUserList {
}
.forumTitle {
  margin-top: 10px;
  margin-bottom: 10px;
  font-size: 12px;
  font-weight: 500;
  color: #181818;
  line-height: 18px;
  display: -webkit-box;
  text-overflow: ellipsis;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
}
.forumBottom {
  display: flex;
  justify-content: space-between;
  width: 100%;
}
.forumLeft {
  display: flex;
}
.userHead {
  width: 19px;
  height: 19px;
  border-radius: 19px;
  align-items: center;
}
.userName {
  font-size: 10px;
  font-weight: 300;
  color: #666666;
  margin-left: 5px;
  margin-top: 2px;
}
.forumRight {
}
.redHeart {
  width: 13px;
  height: 11px;
}
.loveNumber {
  font-size: 10px;
  font-weight: 300;
  color: #666666;
}
.right {
  width: 48.5%;
  float: right;
}
.sendInvitationWrap {
  width: 45px;
  font-weight: 500;
  color: #ffffff;
  line-height: 20px;
  position: fixed;
  top: 65vh;
  right: 5px;
  font-size: 12px;
}
.sendItem {
  text-align: center;
  width: 45px;
  height: 45px;
  line-height: 45px;
  background: #f48f5b;
  border-radius: 50%;
  margin-bottom: 15px;
}
.msgItem {
  text-align: center;
  width: 45px;
  height: 45px;
  line-height: 45px;
  background: #181818;
  border-radius: 50%;
  position: relative;
}
.msgText {
}
.redPoint {
  position: absolute;
  top: 4px;
  right: 0px;
  width: 9px;
  height: 9px;
  background: #ff5656;
  border-radius: 50%;
}

/* 这里取消置顶样式 */
.center{
  width: 100%;
  margin-bottom: 30px;
}
.items{
position: relative;

}
.top{
  border-radius: 15px;
  width: 50px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  background: #a26a6a;
  position: absolute;
  right: 8px;
  top: 20px;
  z-index: 5;
  color: #fff;
}

.pulldown{
  position: fixed;
  bottom: 110px;
  left: 30px;

  border-radius: 50%;

}
.img_back{
  width: 40rpx;
  height: 40px;
}

</style>
